package com.hz.foodstalls.controller;

import com.hz.foodstalls.interceptor.AdminInterceptor;
import com.hz.foodstalls.model.WechatInfo;
import com.hz.foodstalls.model.WexinMsg;
import com.jfinal.aop.Before;
import com.jfinal.ext.render.excel.PoiRender;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Page;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * WexinMsgController, Generated by Robot on 2017-8-22 13:59:49.
 */
@Before(AdminInterceptor.class)
@ControllerBind(controllerKey = "/wexinmsg", viewPath = "/WEB-INF/views/wexinmsg/")
public class WexinMsgController extends BaseController {

    private final String MODULE_NAME = "/wexinmsg";

    public void index() {
        String reservation = getPara("reservation");
        String starttime = null;
        String endtime = null;
        if (StringUtils.isNotBlank(reservation)) {
            String[] time = reservation.split(" - ");
            starttime = time[0];
            endtime = time[1] + " 23:59:59";
        }
        String content = getPara("content");
        String id = getPara("id");
        int ids = Integer.parseInt(id);
        Page<WexinMsg> page = WexinMsg.dao.page(getParaToInt("pageNumber", 1), getParaToInt("pageSize", 10), ids,
                content, starttime, endtime);
        setAttr("page", page);
        WechatInfo wechatInfo = WechatInfo.dao.findFirst("select * from wechat_info where id=?", id);
        setAttr("wx", wechatInfo);
        setAttr("id", id);
        keepPara();
        render("index.html");
    }

    public void save() {
        WexinMsg model = getModel(WexinMsg.class, "");
        boolean isSave = model.getId() == null;

        if (WexinMsg.dao.saveOrUpdate(model, isSave))
            redirect(MODULE_NAME);
        else
            renderError(501);
    }

    public void edit() {
        WexinMsg wexinMsg = WexinMsg.dao.findById(getParaToInt("id", 0));
        if (wexinMsg == null)
            wexinMsg = new WexinMsg();

        setAttr("wexinMsg", wexinMsg);
        render("edit.html");
    }

    public void delete() {
        String ids = StringUtils.join(getParaValues("ids"), ",");
        int ret = WexinMsg.dao.batchDelete(ids);
        if (ret > 0) {
            ajaxJsonSuccessMessage("删除成功!");
        } else {
            ajaxJsonErrorMessage("删除失败！");
        }
    }

    public void exportExcel() {
        // 查询得到list
        String content = getPara("content");
        String reservation = getPara("reservation");
        String starttime = null;
        String endtime = null;
        if (StringUtils.isNotBlank(reservation)) {
            String[] time = reservation.split(" - ");
            starttime = time[0];
            endtime = time[1] + " 23:59:59";
        }
        String where = String.format(" wechat_info_id = %d  %s %s ", getParaToInt("id"),
                StringUtils.isBlank(content) ? "" : "and content like '%" + content + "%'",
                (starttime == "" && endtime == "") || (starttime == null && endtime == null) ? ""
                        : "and createtime between '" + starttime + "' and '" + endtime + "'");
        List<WexinMsg> list = WexinMsg.dao.find("select * from wexin_msg where " + where + " order by id desc");
        Map users = new HashMap();
        List<WexinMsg> data = new ArrayList<>();
        for (WexinMsg wm : list) {
            if (!users.containsKey(wm.getFromUserName())) {
                users.put(wm.getFromUserName(), wm.getFromUserName());
                String phone = wm.getContent();
                if (StringUtils.isNoneBlank(phone)) {
                    String[] phones = phone.split("#");
                    String realphone = phones.length > 1 ? phones[1] : phone;
                    wm.setContent(realphone);
                    data.add(wm);
                }
            }
        }

        PoiRender poi = PoiRender.me(data).headers(new String[]{"联系方式"}).sheetName("Sheet1").fileName("temp.xlsx")
                .headerRow(1).columns(new String[]{"content"});
        render(poi);
    }
}
